package utils;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.commons.lang3.time.DateUtils;


/**
 * 时间方法
 * @author xuyang
 *
 */
public class Time {
	public static final SimpleDateFormat DATE_TIME_FORMAT = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );

	private static int weeks = 0;


	/**
	 * 查询当前毫秒值
	 * @return
	 */
	public static long getCurrentTTimeMilis()
	{
		Date	dt	= new Date();
		Long	time	= dt.getTime();
		return(time);
	}


	/**
	 * 按照规定格式获取当前时间
	 * @param format
	 * @return
	 */
	public static String getTimeWithFmt( DateFormat format )
	{
		return(getTimeWithFmt( new Date(), format ) );
	}


	/**
	 * 按照规定格式获取指定的时间
	 * @param format
	 * @return
	 */
	public static String getTimeWithFmt( Date dateTime, DateFormat format )
	{
		if ( null == dateTime )
		{
			dateTime = new Date();
		}
		if ( null == format )
		{
			format = DATE_TIME_FORMAT;
		}
		return(format.format( dateTime ) );
	}

	public static Date formatTimeWithFmt( String dateTime, DateFormat format ) throws ParseException
	{
		if ( null == format )
		{
			format = DATE_TIME_FORMAT;
		}
		return format.parse(dateTime);
	}

	/**
	 * 今天
	 *
	 * @return
	 */
	public static String getTime()
	{
		SimpleDateFormat	dft		= new SimpleDateFormat( "yyyy-MM-dd" );
		Date			beginDate	= new Date();
		Calendar		date		= Calendar.getInstance();
		date.setTime( beginDate );
		date.set( Calendar.DATE, date.get( Calendar.DATE ) );
		String etime = "";
		try {
			Date			endDate = dft.parse( dft.format( date.getTime() ) );
			SimpleDateFormat	sdf	= new SimpleDateFormat( "yyyy-MM-dd" );
			etime = sdf.format( endDate );
		} catch ( ParseException e ) {
			/* TODO Auto-generated catch block */
			e.printStackTrace();
		}

		return(etime);
	}


	/**
	 * 今天 带时分秒
	 *
	 * @return
	 */
	public static String getTimeHMS()
	{
		SimpleDateFormat	dft		= new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
		Date			beginDate	= new Date();
		Calendar		date		= Calendar.getInstance();
		date.setTime( beginDate );
		date.set( Calendar.DATE, date.get( Calendar.DATE ) );
		String etime = "";
		try {
			Date			endDate = dft.parse( dft.format( date.getTime() ) );
			SimpleDateFormat	sdf	= new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
			etime = sdf.format( endDate );
		} catch ( ParseException e ) {
			/* TODO Auto-generated catch block */
			e.printStackTrace();
		}

		return(etime);
	}


	/**
	 * 前一天
	 *
	 * @return
	 */
	public static String getTimes()
	{
		SimpleDateFormat	dft		= new SimpleDateFormat( "yyyy-MM-dd" );
		Date			beginDate	= new Date();
		Calendar		date		= Calendar.getInstance();
		date.setTime( beginDate );
		date.set( Calendar.DATE, date.get( Calendar.DATE ) - 1 );
		String etime = "";
		try {
			Date			endDate = dft.parse( dft.format( date.getTime() ) );
			SimpleDateFormat	sdf	= new SimpleDateFormat( "yyyy-MM-dd" );
			etime = sdf.format( endDate );
		} catch ( ParseException e ) {
			/* TODO Auto-generated catch block */
			e.printStackTrace();
		}

		return(etime);
	}


	/**
	 * 获取所传时间前一天
	 * @param time
	 * @return
	 */
	public static String getSpecifiedDayBefore( String time )
	{
		Calendar	c	= Calendar.getInstance();
		Date		date	= null;
		try {
			date = new SimpleDateFormat( "yy-MM-dd" ).parse( time );
		} catch ( ParseException e ) {
			e.printStackTrace();
		}
		c.setTime( date );
		int day = c.get( Calendar.DATE );
		c.set( Calendar.DATE, day - 1 );
		String dayBefore = new SimpleDateFormat( "yyyy-MM-dd" ).format( c.getTime() );
		return(dayBefore);
	}


	public static String getSpecifiedDayBefore2( String time )
	{
		Calendar	c	= Calendar.getInstance();
		Date		date	= null;
		try {
			date = new SimpleDateFormat( "yy-MM-dd" ).parse( time );
		} catch ( ParseException e ) {
			e.printStackTrace();
		}
		c.setTime( date );
		int day = c.get( Calendar.DATE );
		c.set( Calendar.DATE, day - 2 );
		String dayBefore = new SimpleDateFormat( "yyyy-MM-dd" ).format( c.getTime() );
		return(dayBefore);
	}


	/**
	 * 判断今天是星期几 weekday=1，当天是周 一；weekday=2，当天是周二；...;weekday=7，当天是周日。
	 *
	 * @return
	 */
	public static String getTimeWeekDay( String time )
	{
		SimpleDateFormat	format		= new SimpleDateFormat( "yyyy-MM-dd" );
		Calendar		c		= Calendar.getInstance();
		int			dayForWeek	= 0;
		try {
			c.setTime( format.parse( time ) );
			if ( c.get( Calendar.DAY_OF_WEEK ) == 1 )
			{
				dayForWeek = 7;
			} else {
				dayForWeek = c.get( Calendar.DAY_OF_WEEK ) - 1;
			}
		} catch ( ParseException e ) {
			/* TODO Auto-generated catch block */
			e.printStackTrace();
		}
		return(dayForWeek + "");
	}


	/**
	 * 获取上月第一天日期
	 *
	 * @return
	 */
	public static String getPreviousMonthFirst()
	{
		String str = "";

		SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );

		Calendar lastDate = Calendar.getInstance();

		lastDate.set( 5, 1 );

		lastDate.add( 2, -1 );

		str = sdf.format( lastDate.getTime() );

		return(str);
	}


	/**
	 * 获得本周一
	 * @return
	 */
	private static int getMondayPlus()
	{
		Calendar cd = Calendar.getInstance();

		int dayOfWeek = cd.get( 7 ) - 1;

		if ( dayOfWeek == 1 )
		{
			return(0);
		}

		return(1 - dayOfWeek);
	}


	/**
	 * 获取上周日日期
	 *
	 * @return
	 */
	public static String getPreviousWeekSunday()
	{
		weeks = 0;

		weeks -= 1;

		int mondayPlus = getMondayPlus();

		GregorianCalendar currentDate = new GregorianCalendar();

		currentDate.add( 5, mondayPlus + weeks );

		Date			monday		= currentDate.getTime();
		SimpleDateFormat	sdf		= new SimpleDateFormat( "yyyy-MM-dd" );
		String			preMonday	= sdf.format( monday );

		return(preMonday);
	}


	/**
	 * 获取本周日日期
	 *
	 * @return
	 */
	public static String getPreviousWeekSundays()
	{
		weeks = 0;

		int mondayPlus = getMondayPlus();

		GregorianCalendar currentDate = new GregorianCalendar();

		currentDate.add( 5, mondayPlus + 6 );

		Date monday = currentDate.getTime();

		SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );

		String preMonday = sdf.format( monday );

		return(preMonday);
	}


	/**
	 * 获取上周第一天日期
	 *
	 * @return
	 */
	public static String getPreviousWeekday()
	{
		weeks	= 0;
		weeks	-= 1;

		int mondayPlus = getMondayPlus();

		GregorianCalendar currentDate = new GregorianCalendar();

		currentDate.add( 5, mondayPlus + 7 * weeks );

		Date monday = currentDate.getTime();

		SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );

		String preMonday = sdf.format( monday );

		return(preMonday);
	}


	/**
	 * 获取上月最后一天日期
	 *
	 * @return
	 */
	public static String getPreviousMonthEnd()
	{
		String			str		= "";
		SimpleDateFormat	sdf		= new SimpleDateFormat( "yyyy-MM-dd" );
		Calendar		lastDate	= Calendar.getInstance();
		lastDate.add( 2, -1 );
		lastDate.set( 5, 1 );
		lastDate.roll( 5, -1 );
		str = sdf.format( lastDate.getTime() );
		return(str);
	}


	/**
	 * 返回当前年份
	 *
	 * @return
	 */
	public static String getTimeYear()
	{
		SimpleDateFormat	dft		= new SimpleDateFormat( "yyyy" );
		Date			beginDate	= new Date();
		Calendar		date		= Calendar.getInstance();
		date.setTime( beginDate );
		date.set( Calendar.DATE, date.get( Calendar.DATE ) );
		String etime = "";
		try {
			Date			endDate = dft.parse( dft.format( date.getTime() ) );
			SimpleDateFormat	sdf	= new SimpleDateFormat( "yyyy" );
			etime = sdf.format( endDate );
		} catch ( ParseException e ) {
			/* TODO Auto-generated catch block */
			e.printStackTrace();
		}

		return(etime);
	}


	/**
	 * 返回所传时间为第几周
	 *
	 * @param time
	 * @return
	 */
	public static String getTimeWeek( String time )
	{
		SimpleDateFormat	format = new SimpleDateFormat( "yyyy-MM-dd" );
		Date			date;
		int			day = 0;
		try {
			date = format.parse( time );
			Calendar calendar = Calendar.getInstance();
			calendar.setFirstDayOfWeek( Calendar.MONDAY );
			calendar.setTime( date );
			day = calendar.get( Calendar.WEEK_OF_YEAR );
		} catch ( ParseException e ) {
			/* TODO Auto-generated catch block */
			e.printStackTrace();
		}
		return(day + "");
	}


	/**
	 * 两个时间相差距离多少天多少小时多少分多少秒
	 *
	 * @param str1
	 *            时间参数 1 格式：1990-01-01 12:00:00
	 * @param str2
	 *            时间参数 2 格式：2009-01-01 12:00:00
	 * @return long[] 返回值为：{天, 时, 分, 秒}
	 */
	public static long[] getDistanceTimes( String str1, String str2 )
	{
		DateFormat	df = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
		Date		one;
		Date		two;
		long		day	= 0;
		long		hour	= 0;
		long		min	= 0;
		long		sec	= 0;
		try {
			one	= df.parse( str1 );
			two	= df.parse( str2 );
			long	time1	= one.getTime();
			long	time2	= two.getTime();
			long	diff;
			if ( time1 < time2 )
			{
				diff = time2 - time1;
			} else {
				diff = time1 - time2;
			}
			day	= diff / (24 * 60 * 60 * 1000);
			hour	= (diff / (60 * 60 * 1000) - day * 24);
			min	= ( (diff / (60 * 1000) ) - day * 24 * 60 - hour * 60);
			sec	= (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		} catch ( ParseException e ) {
			e.printStackTrace();
		}
		long[] times = { day, hour, min, sec };
		return(times);
	}


	/**
	 * 两个时间相差距离多少天多少小时多少分多少秒
	 *
	 * @param str1
	 *            时间参数 1 格式：1990-01-01 12:00:00
	 * @param str2
	 *            时间参数 2 格式：2009-01-01 12:00:00
	 * @return String 返回值为：xx天xx小时xx分xx秒
	 */
	public static String getDistanceTime( String str1, String str2 )
	{
		DateFormat	df = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
		Date		one;
		Date		two;
		long		day	= 0;
		long		hour	= 0;
		long		min	= 0;
		long		sec	= 0;
		try {
			one	= df.parse( str1 );
			two	= df.parse( str2 );
			long	time1	= one.getTime();
			long	time2	= two.getTime();
			long	diff;
			if ( time1 < time2 )
			{
				diff = time2 - time1;
			} else {
				diff = time1 - time2;
			}
			day	= diff / (24 * 60 * 60 * 1000);
			hour	= (diff / (60 * 60 * 1000) - day * 24);
			min	= ( (diff / (60 * 1000) ) - day * 24 * 60 - hour * 60);
			sec	= (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		} catch ( ParseException e ) {
			e.printStackTrace();
		}
		return(day + "天" + hour + "小时" + min + "分" + sec + "秒");
	}


	/**
	 * 获取所传时间周一日期
	 */
	public static String getNowWeekMonday( String date )
	{
		DateFormat	df	= new SimpleDateFormat( "yyyy-MM-dd" );
		Calendar	cal	= Calendar.getInstance();
		try {
			cal.setTime( df.parse( date ) );
		} catch ( ParseException e ) {
			/* TODO Auto-generated catch block */
			e.printStackTrace();
		}

		cal.add( Calendar.DAY_OF_MONTH, -1 ); /* 解决周日会出现 并到下一周的情况 */
		cal.set( Calendar.DAY_OF_WEEK, Calendar.MONDAY );

		return(df.format( cal.getTime() ) );
	}


	/**
	 * 获取所传时间上周周一日期
	 */
	public static String getLastWeekMonday( String date )
	{
		DateFormat	df	= new SimpleDateFormat( "yyyy-MM-dd" );
		Date		a	= null;
		try {
			a = DateUtils.addDays( df.parse( date ), -1 );
		} catch ( ParseException e ) {
			/* TODO Auto-generated catch block */
			e.printStackTrace();
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime( a );
		cal.add( Calendar.WEEK_OF_YEAR, -1 ); /* 一周 */
		cal.set( Calendar.DAY_OF_WEEK, Calendar.MONDAY );

		return(df.format( cal.getTime() ) );
	}


	/**
	 * 获取所传时间上周周日日期
	 */
	public static String getLastWeekSunday( String date )
	{
		DateFormat	df	= new SimpleDateFormat( "yyyy-MM-dd" );
		Date		a	= null;
		try {
			a = DateUtils.addDays( df.parse( date ), -1 );
		} catch ( ParseException e ) {
			/* TODO Auto-generated catch block */
			e.printStackTrace();
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime( a );
		cal.set( Calendar.DAY_OF_WEEK, 1 );

		return(df.format( cal.getTime() ) );
	}

	public static String getLastWeekSunday2( String date )
	{
//		System.err.println(date);
		SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
		try {
			String strTime = new SimpleDateFormat( "yyyy年MM月dd日" ).format( format.parse( date ) );
			return(strTime);
		} catch ( ParseException e ) {
			/*
			 * e.printStackTrace();
			 * 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
			 */
			return(date);
		}
	}
	public static String isValidDate( String str )
	{
		/* 指定日期格式为四位年/两位月份/两位日期，注意yyyy/MM/dd区分大小写； */
		SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss.S" );
		try {
			/* 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01 */
			String strTime = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ).format( format.parse( str ) );
			return(strTime);
		} catch ( ParseException e ) {
			/*
			 * e.printStackTrace();
			 * 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
			 */
			return(str);
		}
	}
	
	/**
	 * 将所传时间字符串转换成long形式
	 * @param time
	 * @return
	 */
	public static long getStringTimeToLong (String time){
		return Long.valueOf(time.replaceAll("[-\\s:]",""));
	}
}
